/*
 * Main module file which defines the attribute of the module and the startup and exit codes during module load and unload
 *
 *      Author: Aboo Valappil
 *      Date: 2/10/2008
 *      Based on: iSCSI initiator
 */

#include "ndmp_main.h"

MODULE_AUTHOR("Aboo Valappil");
MODULE_DESCRIPTION("ndmphba");
MODULE_LICENSE("GPL");
MODULE_VERSION(NDMP_HBA_VERSION);

struct ndmp_scsi_sessions sessions; // Global sessions list
struct kmem_cache *ndmp_scsi_task_cache;

int __init ndmp_scsi_init(void) {

        int major;

	// Create the slab cache for SCSI tasks as every scsi comamnd will have a task
        ndmp_scsi_task_cache = KMEM_CACHE(ndmp_scsi_task,0);

        if (!ndmp_scsi_task_cache) {
                return -ENOMEM;
        }

	// Register the user interface device and initialize the global sessions structure
        if ( (major=ndmp_scsi_register_user_interface()) ) {
                printk("ndmphba: User control interface device Major=%d\n",major);
                INIT_LIST_HEAD(&(sessions.session_queue));
                sema_init(&(sessions.session_lock), 1);
		sessions.dev_major=major;
		if (!ndmp_create_kset()) { // Create the sysfs object for ndmphba
        		sessions.last_id=0;
        		return 0;
		}
        }

	// Destroy the slab allocation if above operation failed.
        kmem_cache_destroy(ndmp_scsi_task_cache);
        printk ("ndmphba: Failed to init the driver\n");

        return -ENODEV;
}

void __exit ndmp_scsi_cleanup(void)
{

	// Destroy all the allocated objects during module unload: slab allocation, user interface device and sysfs object
        kmem_cache_destroy(ndmp_scsi_task_cache);
        ndmp_scsi_unregister_user_interface();
	ndmp_scsi_kset_remove(); 
        printk("ndmphba: Driver removed due to module unload\n");
}

module_init(ndmp_scsi_init);
module_exit(ndmp_scsi_cleanup);

